package thread;

import sun.util.resources.ga.LocaleNames_ga;

import java.util.concurrent.CountDownLatch;

/**
 * @Author zhangxuhui
 * @Date 2023/12/20
 * @email zxh_1633@163.com
 * cpu 缓存行 64byte
 * 缓存一致性协议
 * 21524ms 6495ms
 */
public class CacheLinePadding {

    public static long COUNT = 10_0000_0000L;

    private static class T{
        private long p1,p2,p3,p4,p5,p6,p7;
        public volatile long x = 0L;
        private long p8,p9,p10,p11,p12,p13,p14;

    }

    public static T[] arr = new T[2];

    static {
        arr[0] = new T();
        arr[1] = new T();
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(2);

        Thread t1 = new Thread(() -> {
            for (long i = 0; i < COUNT; i++) {
                arr[0].x = i;
            }
            countDownLatch.countDown();
        });

        Thread t2 = new Thread(() -> {
            for (long i = 0; i < COUNT; i++) {
                arr[1].x = i;
            }
            countDownLatch.countDown();
        });

        long start = System.currentTimeMillis();
        t1.start();
        t2.start();
        countDownLatch.await();
        long end = System.currentTimeMillis();
        System.out.println("耗时："+(end - start) +"ms");
    }
}
